<?php
require_once ROOT_PATH.'boot/req_inc.php';

//web页面模板处理
if(!defined('WEB_PAGE_TPL')) define('WEB_PAGE_TPL','web_page.tpl.php'); //web页面模板
if(!defined('WEB_PAGE_ERR_TPL')) define('WEB_PAGE_ERR_TPL','web_page_error.tpl.php'); //错误页面模板
if(! defined('RUN_WIDGET') || ! RUN_WIDGET){
	function render_widget($widget_id){}
}
if(! defined('RUN_CONTROL') || ! RUN_CONTROL){
	function render_control($clazz,$id,array $options=array()){}
}
function get_web_page($tpl_file, $page='') {
	$WEB_PAGE_TPL = WEB_PAGE_TPL;
	if($page) $WEB_PAGE_TPL = str_replace('web_page', 'web_page_'.$page, $WEB_PAGE_TPL);
	$dirpath = dirname ( $tpl_file );
	if (! $dirpath)	return $WEB_PAGE_TPL;
	if (DS != '/')	$dirpath = str_replace ( '/', DS, $dirpath );
	if ($dirpath == '/' || $dirpath == '/')	return $dirpath . $WEB_PAGE_TPL;
	if (file_exists ( $dirpath . DS . $WEB_PAGE_TPL )) return $dirpath . DS . $WEB_PAGE_TPL;

	$ctx = $GLOBALS ['context'];
	$views_path = ROOT_PATH . $ctx->app_name . DS . 'views';
	if ($ctx->theme) $views_path .= DS . $ctx->theme;
	if( defined('RUN_MLANG_VIEW') && RUN_MLANG_VIEW) $views_path.=$ctx->app_lang .DS ;

	while ( $dirpath && $dirpath != DS && $dirpath != $views_path ) {
		$pos = strrpos ( $dirpath, DS );
		$dirpath = substr ( $dirpath, 0, $pos );
		if (file_exists ( $dirpath . DS . $WEB_PAGE_TPL )) return $dirpath . DS . $WEB_PAGE_TPL;
	}
}
if(! function_exists('get_tpl_path')){
	function get_tpl_path($tplname) {
		global $context;
		$views_path=ROOT_PATH . $context->app_name . DS.'views'. DS ;
		if($context->theme) $views_path.=$context->theme .DS ;
		if( defined('RUN_MLANG_VIEW') && RUN_MLANG_VIEW) $views_path.=$context->app_lang .DS ;
		if('/'!==DS) $tplname=str_replace('/',DS,$tplname);
		return $views_path.$tplname.'.tpl.php';
	}
}
/**
 * 放置分页条
 * @param array  $page_info 分页信息   TableMapper->get_page_info函数返回值,@see TableMapper
 * @param string $page_link  页码链接模板
 * @param string $page_tpl   分页条模板文件，在views模板目录的pagination子目录下，默认值page，即page.tpl.php文件。
 */
function put_pagination(array $page_info, $page_link, $page_tpl = 'page') {
	$_fa_file_X3f7t_o0z8 = get_tpl_path ( 'pagination' . DS . $page_tpl );
	if (! file_exists ( $_fa_file_X3f7t_o0z8 )) {
		global $context;
		$context->log_error ( "pagination [{$_fa_file_X3f7t_o0z8}] NOT FOUND" );
	} else {
		unset ( $page_tpl );
		include $_fa_file_X3f7t_o0z8;
	}
}

//end web页面模板处理

class PluginHtmlRenderer implements IReponseRenderer {
	private function get_web_page($tpl_file, $other_web_page = '') {
		$WEB_PAGE_TPL = 'web_page.tpl.php';
		if($other_web_page) $WEB_PAGE_TPL = str_replace('web_page', 'web_page_'.$other_web_page, $WEB_PAGE_TPL);
		$file = ROOT_PATH.$context->app_name.'/plugins'.$tpl_file.'views/'.$WEB_PAGE_TPL;
		if(file_exists($file)){
			return $file;
		} else {
			return false;
		}
	}
	private function render_error(array & $app,$view_path,$none_page){
		if($app['err_no']==0) return;
		if(! file_exists( $view_path. WEB_PAGE_ERR_TPL)){
			$GLOBALS['context']->log_error ( 'ERROR PAGE TEMPLATE '.$view_path. WEB_PAGE_ERR_TPL .' NOT FOUND!'  );
			echo '<html><head>';
			if(isset($app['title'])) 	echo '<title>'.$app['title'].'</title></head><body>';
 			echo '<h1>ERROR PAGE TEMPLATE NOT FOUND!</h1></body></html>';
 			exit;
		}
		if($none_page) include $view_path. WEB_PAGE_ERR_TPL;
		else{	//add head and tail for app_page=null,WEB_PAGE_ERR_TPL is not full page
			/*
			echo "<html><head>\n<meta http-equiv='content-Type' content='text/html; charset=".
				$GLOBALS['context']->get_app_conf('charset') ."'/>\n";
			if(isset($app['title'])) 	echo '<title>'.$app['title']."</title></head><body>";
 			include $view_path. WEB_PAGE_ERR_TPL;
			echo "\n</body></html>";
			*/
			include $view_path. WEB_PAGE_ERR_TPL;
		}
		exit;
	}
	function render(array & $request,array & $response,  array & $app) {
		if ($app ['fmt'] !== 'pluginhtml')  return;

		$context=$GLOBALS['context'];
		$webpage_charset=$context->get_app_conf('charset');
		$_none_page=isset($app['page'])&& strcasecmp($app['page'],'NULL')==0;
		$app['tpl'] = isset($app['tpl']) ? $app['tpl'] .'.tpl.php': 'index.tpl.php';
		$objPlugin = $app['plugin_path'];
		$main_child_tpl = ROOT_PATH.$context->app_name.'/plugins'.$objPlugin.'views/'.$app['tpl'];
		$_views_path = ROOT_PATH.$context->app_name.'/plugins'.$objPlugin.'views/';
		$res = $response['result'];
		if(! file_exists($main_child_tpl)){
			$app['err_no']=20001;
			$app['err_msg']=lang('req_err_not_found_tpl').'['. $main_child_tpl.']';
			$this->render_error($app,$_views_path,$_none_page);
		}
		if ($_none_page) {
			$_FASTAPP_web_page_file_3D8jKw5L2q=$main_child_tpl;
			unset($main_child_tpl);
			include $_FASTAPP_web_page_file_3D8jKw5L2q;
			return true;
		} else {
			ob_start();

			if (! isset ( $app ['page'] )) $_web_page_file = get_web_page ( $main_child_tpl );
			else{
				$_web_page_file = get_web_page ( $main_child_tpl, $app ['page'] );
				if($_web_page_file)  {
        			$_FASTAPP_web_page_file_3D8jKw5L2q  = $_web_page_file;
        		}
        		if(file_exists($_FASTAPP_web_page_file_3D8jKw5L2q)) {
        			include $_FASTAPP_web_page_file_3D8jKw5L2q;
        		} else {
        			$_FASTAPP_web_page_file_3D8jKw5L2q = $main_child_tpl;
        			echo "<html><head>\n<meta http-equiv='content-Type' content='text/html; charset=".
					$GLOBALS['context']->get_app_conf('charset')."' />\n";
					echo  "</head><body>\n";
				include $_FASTAPP_web_page_file_3D8jKw5L2q;
					echo "\n</body></html>";
        		}
			}
			$html = ob_get_contents();
			ob_end_clean();
			echo $html;
			return true;
		}
	}
}

